package com.example.tobebigfe

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        WebView.setWebContentsDebuggingEnabled(true);

        webView.settings.javaScriptEnabled = true
        webView.webViewClient = WebViewClient()

        // 在加载网页前添加我们的js对象
        webView.addJavascriptInterface(BridgeObject(webView), "androidBridge")

        // 加载assets中的网页
        webView.loadUrl("file:///android_asset/test.html")
    }

    inner class BridgeObject(val webView: WebView) {

        @JavascriptInterface
        fun callNative(callbackId: String, arg: String) {
            Log.e("WebView", "callNative ok. args is $arg")
            webView.post {
                webView.evaluateJavascript("window.$callbackId('Native callback ok.')", null)
            }
        }
    }
}
